/**
 * @func 存储校验options配置信息
 * 
 *  */
import { validateOption } from '../utils/tools'
import { _support } from '../utils/global'
import { InitOptions } from '../interfaces/options'
// 格式化options
class Options {
    dsn = ''; // 监控上报接口的地址 
    throttleDelayTime = 0; // click事件的节流时长
    overTime = 10; // 接口超时时长
    whiteBoxElements: string[] = ['html', 'body', '#app', '#root']; // // 白屏检测的容器列表
    silentWhiteScreen = false; // 是否开启白屏检测
    skeletonProject = false; // 项目是否有骨架屏
    filterXhrUrlRegExp: any; // 过滤的接口请求正则
    handleHttpStatus: any; // 处理接口返回的 response
    repeatCodeError = false; // 是否去除重复的代码错误，重复的错误只上报一次

    apikey = ''; // 每个项目对应的唯一标识
    userId = ''; // 用户id
    uuid: string; // 每次页面加载的唯一标识
    beforeDataReport: any; // 上报数据前的hook
    getUserId: any; // 用户自定义获取userId的方法
    useImgUpload = false; // 是否使用图片打点上报

    maxBreadcrumbs = 20; // 用户行为存放的最大长度
    beforePushBreadcrumb: unknown = null;//添加用户行为前的处理函数

    optionsList: InitOptions;
    constructor() {
        this.optionsList = {} as InitOptions
    }
    bindOptions(options) {
        const {
            dsn,
            filterXhrUrlRegExp,
            throttleDelayTime = 0,
            overTime = 10,
            silentWhiteScreen = false,
            whiteBoxElements = ['html', 'body', '#app', '#root'],
            skeletonProject = false,
            handleHttpStatus,
            repeatCodeError = false,
            apikey,
            beforeDataReport,
            userId,
            getUserId,
            useImgUpload
        } = options;

        // 判断配置的类型是否正确且赋值
        validateOption(dsn, 'dsn', 'string') && (this.dsn = dsn);

        validateOption(throttleDelayTime, 'throttleDelayTime', 'number') &&
            (this.throttleDelayTime = throttleDelayTime);

        validateOption(overTime, 'overTime', 'number') && (this.overTime = overTime);

        validateOption(filterXhrUrlRegExp, 'filterXhrUrlRegExp', 'regexp') &&
            (this.filterXhrUrlRegExp = filterXhrUrlRegExp);

        validateOption(silentWhiteScreen, 'silentWhiteScreen', 'boolean') &&
            (this.silentWhiteScreen = silentWhiteScreen);

        validateOption(skeletonProject, 'skeletonProject', 'boolean') &&
            (this.skeletonProject = skeletonProject);

        validateOption(whiteBoxElements, 'whiteBoxElements', 'array') &&
            (this.whiteBoxElements = whiteBoxElements);

        validateOption(handleHttpStatus, 'handleHttpStatus', 'function') &&
            (this.handleHttpStatus = handleHttpStatus);

        validateOption(repeatCodeError, 'repeatCodeError', 'boolean') &&
            (this.repeatCodeError = repeatCodeError);

        validateOption(apikey, 'apikey', 'string') && (this.apikey = apikey);

        validateOption(userId, 'userId', 'string') && (this.userId = userId || '');

        validateOption(useImgUpload, 'useImgUpload', 'boolean') &&
            (this.useImgUpload = useImgUpload || false);

        validateOption(beforeDataReport, 'beforeDataReport', 'function') &&
            (this.beforeDataReport = beforeDataReport);

        validateOption(getUserId, 'getUserId', 'function') && (this.getUserId = getUserId);


        const { maxBreadcrumbs, beforePushBreadcrumb } = options;
        validateOption(maxBreadcrumbs, 'maxBreadcrumbs', 'number') &&
            (this.maxBreadcrumbs = maxBreadcrumbs || 20);
        validateOption(beforePushBreadcrumb, 'beforePushBreadcrumb', 'function') &&
            (this.beforePushBreadcrumb = beforePushBreadcrumb);
    }
}
// 导出options
const options = _support.options || (_support.options = new Options())

// 存储options
export function saveOptions(optionParams) {
    // 初始化
    options.bindOptions(optionParams)
}


export { options }